﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace _1241118_DoAn_hqtcsdl
{
    public partial class DangNhap : Form
    {
        string connect = @"Data Source=QUAN\QUAN1504;Initial Catalog=QuanLyGaSG;Integrated Security=True";

        public DangNhap()
        {
            InitializeComponent();
        }
        int ktdangnhap(string username, string password)
        {
            SqlConnection sql = new SqlConnection(connect);
            string sqlcomman = "select * from NhanVien where MaNV = '" + username + "' and MatKhau = '" + password + "'";
            sql.Open();
            SqlCommand sqlcm = new SqlCommand(sqlcomman,sql);
            SqlDataReader sqldtrd = sqlcm.ExecuteReader();
            if (sqldtrd.Read()) //kt dòng: có return 1
                 return 1;
            return 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (ktdangnhap(textBox1.Text, textBox2.Text) == 1)
            {
                SqlConnection sql = new SqlConnection(connect);
                string sqlcomman = "select MaChucVu from NhanVien where MaNV = '" + textBox1.Text + "' and MatKhau = '" + textBox2.Text + "'";
                sql.Open();
                SqlCommand sqlcm = new SqlCommand(sqlcomman, sql);
                SqlDataReader sqldtrd = sqlcm.ExecuteReader();
                string machucvu = "";
                while (sqldtrd.Read()) //kt dòng: có return 1
                {
                    machucvu = sqldtrd["MaChucVu"].ToString();
                }

                if (machucvu == "TG        ")
                {
                    TruongGa f2 = new TruongGa();
                    f2.Show();
                    this.Hide();
                    return;
                }

                if (machucvu == "NVQL      ")
                {
                    NhanVienQuanLy f3 = new NhanVienQuanLy();
                    f3.Show();
                    this.Hide();
                    return;
                }

                if (machucvu == "NVBV      ")
                {
                    Ban_Ve f4 = new Ban_Ve();
                    f4.Show();
                    this.Hide();
                    return;
                }

            }
            else
            {
                MessageBox.Show("Kiểm Tra lại Username và Password");
            }
           
        }

        private void DangNhap_Load(object sender, EventArgs e)
        {

        }

       
    }
}
